package com.rambo.sdh.sys.service.impl;

import com.rambo.sdh.sys.dao.inter.UserDao;
import com.rambo.sdh.sys.dao.inter.po.UserPO;
import com.rambo.sdh.sys.service.inter.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@CacheConfig(cacheNames = "userCache")
public class UserServiceImpl implements UserService {
    private UserDao userDao;

    @Autowired
    public UserServiceImpl(UserDao userDao) {
        this.userDao = userDao;
    }

    @Transactional(readOnly = true)
    @Cacheable(key = "#uuid")
    public UserPO findUserByUuid(String uuid) {
        return userDao.getByKey(uuid);
    }

    @Transactional
    @CachePut(key = "#user.getUuid()")
    public UserPO addUser(UserPO user) {
        return userDao.add(user);
    }

    @Transactional
    @CachePut(key = "#user.getUuid()")
    public UserPO updateUser(UserPO user) {
        return userDao.edit(user);
    }

    @Transactional
    @CacheEvict(key = "#uuid")
    public UserPO deleteUserById(String uuid) {
        return userDao.deleteByKey(uuid);
    }

    @Transactional(readOnly = true)
    public List<UserPO> getUserList(UserPO userPo) {
        return userDao.findByExample(userPo);
    }
}
